package Page.MainPage;

import Controller.MainController;
import Controller.User.UserType;
import Dao.AnnouncementDao;
import DataClass.Announcement;
import DataClass.BookBorrowingRequestRecord;
import Navigation.Navigation;
import Navigation.NavigationController;
import Page.BiggerDetailPage.BiggerDetailPage;
import Page.BiggerDetailPage.BiggerDetailPageViewModel;
import Page.DetailPage.DetailPage;
import Page.DetailPage.DetailPageViewModel;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;

import static Controller.MainController.getUserType;


public class MainPageController implements Initializable {
    @FXML
    private Label getUserNameLabel;
    @FXML
    private TableView<Announcement> AnnouncementTable1;
    @FXML
    private TableView<Announcement> AnnouncementTable2;
    @FXML
    private TableView<Announcement> AnnouncementTable3;

    @FXML
    private TableColumn ATitle1;
    @FXML
    private TableColumn ATime1;
    @FXML
    private TableColumn ATitle2;
    @FXML
    private TableColumn ATime2;
    @FXML
    private TableColumn ATitle3;
    @FXML
    private TableColumn ATime3;
    @FXML
    private Menu LoginInterface;
    @FXML
    private Hyperlink computerCollegeLink;

    @FXML
    private Hyperlink graduateSchoolLink;

    @FXML
    private Hyperlink graduateManagementSystemLink;

    @FXML
    private TableColumn<Announcement,String> ATitle;
    @FXML
    private TableColumn<Announcement,String> ATime;
    @FXML
    private TableColumn<Announcement,String> AContent;

    @FXML
    private void handleComputerCollegeLinkClick(ActionEvent event) {
        openWebLink("https://cs.bit.edu.cn");
    }

    @FXML
    private void handleGraduateSchoolLinkClick(ActionEvent event) {
        openWebLink("https://grd.bit.edu.cn");
    }

    @FXML
    private void handleGraduateManagementSystemLinkClick(ActionEvent event) {
        openWebLink("https://grdms.bit.edu.cn");
    }

    private void openWebLink(String url) {
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().browse(new URI(url));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("Desktop not supported");
        }
    }

    public void showDetail(Announcement detail)
    {
        Stage stage = new Stage();
        stage.setTitle(detail.getATitle());
        BiggerDetailPage detailPage = new BiggerDetailPage();
        stage.setScene(detailPage.getScene());
        stage.initOwner(NavigationController.getLastScene().getWindow());
        stage.initModality(Modality.WINDOW_MODAL);
        BiggerDetailPageViewModel.getViewModel().setATitle(detail.getATitle());
        BiggerDetailPageViewModel.getViewModel().setATime(detail.getATime());
        BiggerDetailPageViewModel.getViewModel().setAContent(detail.getAContent());
        stage.showAndWait();
    }

    private MainPageViewModel viewModel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        viewModel = new MainPageViewModel();


        ATime1.setCellValueFactory(new PropertyValueFactory<Announcement,String>("ATime"));
        ATitle1.setCellValueFactory(new PropertyValueFactory<Announcement,String>("ATitle"));
        ATime2.setCellValueFactory(new PropertyValueFactory<Announcement,String>("ATime"));
        ATitle2.setCellValueFactory(new PropertyValueFactory<Announcement,String>("ATitle"));
        ATime3.setCellValueFactory(new PropertyValueFactory<Announcement,String>("ATime"));
        ATitle3.setCellValueFactory(new PropertyValueFactory<Announcement,String>("ATitle"));


        AnnouncementTable1.setItems(viewModel.dataListProperty1());
        AnnouncementTable2.setItems(viewModel.dataListProperty2());
        AnnouncementTable3.setItems(viewModel.dataListProperty3());

        try {
            // 从数据库或其他来源获取数据
            viewModel.setDataList1(AnnouncementDao.getAnnouncement1());
            viewModel.setDataList2(AnnouncementDao.getAnnouncement2());
            viewModel.setDataList3(AnnouncementDao.getAnnouncement3());
        } catch (SQLException e) {
            viewModel.setDataList1(new ArrayList<>());
            System.err.println("Anouncement1 数据获取失败" + e.getMessage());
            viewModel.setDataList2(new ArrayList<>());
            System.err.println("Anouncement2 数据获取失败" + e.getMessage());
            viewModel.setDataList3(new ArrayList<>());
            System.err.println("Anouncement3 数据获取失败" + e.getMessage());
        }

        getUserNameLabel.textProperty().bindBidirectional(viewModel.getHelloProperty());

        AnnouncementTable1.setOnMouseClicked(event ->
        {
            if(event.getClickCount() == 2)
            {
                Announcement data = AnnouncementTable1.getSelectionModel().getSelectedItem();
                Announcement detail = new Announcement(data.getATitle(), data.getATime(), data.getAContent());
                showDetail(detail);
            }
        });

        AnnouncementTable2.setOnMouseClicked(event ->
        {
            if(event.getClickCount() == 2)
            {
                Announcement data = AnnouncementTable2.getSelectionModel().getSelectedItem();
                Announcement detail = new Announcement(data.getATitle(), data.getATime(), data.getAContent());
                showDetail(detail);
            }
        });

        AnnouncementTable3.setOnMouseClicked(event ->
        {
            if(event.getClickCount() == 2)
            {
                Announcement data = AnnouncementTable3.getSelectionModel().getSelectedItem();
                Announcement detail = new Announcement(data.getATitle(), data.getATime(), data.getAContent());
                showDetail(detail);
            }
        });

        MainPageViewModel.updateHelloText();
    }

    /**
     * 处理主页导航事件
     * @param actionEvent MenuItem点击事件
     */
    @FXML
    public void handleMenuAction(ActionEvent actionEvent) {
        MenuItem source = (MenuItem) actionEvent.getSource();
        String text = source.getText();
        System.out.println("to "+text);
        Navigation.ToPage(text);
    }

    @FXML
    public String getUserName(){
        String temp;
        UserType userType = getUserType();
        if (userType != null) {
            if (userType == UserType.TEACHER) {
                temp = "教师";
            } else if (userType == UserType.STUDENT) {
                temp = "学生";
            } else if(userType == UserType.MANAGER)
            {
                temp = "管理员";
            }
        } temp = "游客";
        String temp2 = MainController.getCurrentUsrName();
        if(MainController.getCurrentUsrName()==null){
            temp2 = "";
        }

        return "欢迎" + temp2 + temp + "访问北京理工大学网络攻防对抗技术研究所管理系统！";
    }
}